package modelLayer;

import java.io.IOException;
import java.util.ArrayList;

import controllerLayer.ProductCtr;

import uiLayer.SaveToFile;

import modelLayer.Employee;
import modelLayer.SaleLinesItem;


public class EmployeeRecords {

	
		private static EmployeeRecords instance = null;
		private ArrayList<Employee> employees;
		private ProductCtr pctr;




	public EmployeeRecords() {
		employees = new ArrayList<Employee>();
		pctr = new ProductCtr();
		}

	public static EmployeeRecords getInstance()       
	{
		if(instance == null)
		{
			instance = new EmployeeRecords();
		}
		return instance;
	}
	public void createEmployee(String Id, String name, String address, String phoneNumber,
			double salary, double moneyEarned){
		
		Employee e1 = new Employee(Id,name,address,phoneNumber,salary,moneyEarned);
		employees.add(e1);
	}
	public Employee getEmployee(String empID) {
		for(Employee e1: employees){
			if(e1.getId().equals(empID))return e1;
		}
		
		return null;
	}
	public String findEmployee(String empID) {
		String emp = "Employee doesn't exist";
		for(Employee e1: employees){
			
			if(e1.getId().equals(empID))emp = e1.toString();
		
	}
		return emp;
		}

	public void sellItem(String id,String name,double quantity, String id2, double discount){
		Employee e1 = getEmployee(id);
		SaleLinesItem item = pctr.getSaleLinesItem(id2);
		double price = 0;
		if(item.getItemCollection()!=null)price=item.getPrice();
		else price= item.getProduct().getPrice();
		double total = price*quantity*discount;
		boolean stated = false;
		for(SaleLinesItem i1:e1.getItemsSold()){
			if(item.equals(i1)){
				stated = true;
				
			}
		}
		//if(stated)item.getProduct().setQuantity(item.getProduct().getQuantity()+quantity);			
		if(!stated){
			//item.getProduct().setQuantity(quantity);
			e1.addItemSold(item);
		}
		e1.setMoneyEarned(e1.getMoneyEarned()+ total);
		
	}
	/*
	 * this method sorts employees by money earned
	 */
	public String sortEmployees(){
		int pos = 0;
		String stats = new String(); 
		for(Employee e1: employees){
			int i = employees.indexOf(e1)+1;
			while(i<employees.size()){
				Employee e2 = employees.get(i);
				if(e2.getMoneyEarned()>e1.getMoneyEarned()){
					Employee help = e1;
					e1 = e2;
					e2 = help;
				}
					i++;
				
			}
		}
		for(Employee emp: employees){
			pos++;
			stats = stats + pos + ". "+ emp.toString()+"\n";
		}
		return stats;
		
		
	}

	public void updateEmployee(String address, String phoneNumber, double salary, String empID)
	{
		Employee e1 = getEmployee(empID);
		if(!address.equals(""))e1.setAddress(address);
		if(!phoneNumber.equals(""))e1.setPhoneNumber(phoneNumber);
		if(salary!=0)e1.setSalary(salary);
	}

	public void deleteEmployee(String empID){
		Employee e1 = getEmployee(empID);
		employees.remove(e1);
	}
	public void save() throws IOException{
		for(Employee e1: employees){
			SaveToFile.getInstance().save(e1.save());
			}
			
		}

	public ArrayList<Employee> getEmployees() {
		return employees;
	}

	}
